<?php

?>
<h1 hidden="hidden"> This page Edit product : <?php echo $template->get('product'); ?></h1>
<script type="text/javascript">
    $(document).ready(function(){
        $('#avatar').change(function(evt){
            var f = evt.target.files[0];
            var reader = new FileReader();

            if (!f.type.match('image.*')) {
                alert("The selected file does not appear to be an image.");
                return;
            }
            reader.onload = function (e) {
                var img = $('<img/>');
                img.attr("src", e.target.result).load(function () {
                    $('#image_preview').attr("src",e.target.result);
                });

            };
            reader.readAsDataURL(f);
        });  
    });
    function onchangeSelect()
    {
        //        alert($("select").val());
        $('#dm_name_id').val($("select").val());
    }
    function uploadComplete(img){
        if(img == '')
        {
            img = $('#id_Image').val();
        }
        var nameproduct = $('[name=tensp]').val();
        var price = $('[name=dongia]').val();
        var manufacter = $('[name=dm_name]').val();
        var describe = $('[name=description]').val();
        var category = $('[name=list_categories]').val();
        var subcategory = $('[name=category_uri]').val();
        var id_product = $('#id_product').val();
        var location = $('#location_id').val();
        var str ='nameproduct :' + nameproduct + ' price: '+ price + ' manufacter :' + manufacter +' describe :' +describe +' category :' +category
        + ' subcategory: ' +subcategory +' id_product : ' +id_product ;
       // alert(str);
        if(nameproduct != ''&&price !='' && manufacter !='' && category !='')
        {
            $.post('~product/updateProduct',{idproduct :id_product,nameproduct :nameproduct,price : price,manufacter :manufacter, describe: describe,category : category, subcatalog : subcategory, image : img,location : location} , function(data){
                alert('Update Success');
            });
        }
        else
        {
            alert('Please Fill Out Infor ');
        }

    }
</script>
<?php
    if(isset($template->aParams['error']))
    {
        echo "<h1>ERROR: ".$template->get('error')."</h1>";
    }
?>
<form action="<?php echo URL::getLink('product/edit'); ?>" method="POST" enctype="multipart/form-data" target="upload_target">
    <input hidden="true" id='id_product' value="<?php echo $template->get('product'); ?>"></input>
    <input hidden="true" id='id_Image' value="<?php echo $template->get('image'); ?>"></input>
    <div class="dangspdivimg">
        <img id="image_preview" src="<?php echo URL::getLink('upload/'.$template->get('image')); ?>" alt="" class="dangspimg">
        <input type="file" name="avatar" id="avatar" class="dangspinputfile">
    </div>
    <div class="dangsptitle">
        <div style="margin-left: 20px;">
            <div><p>Tiêu  Đề :<input type="text" name="tensp" value="<?php echo $template->get('name') ?>" style="width: 400px;margin-left: 10px; "></p></div>
            <div>
                <p>Nhà Sản Xuất :
                    <select id="listdm" name="dm_name" style="width: 100px;">
                        <?php
                            $aListmanufacter =$template->get('aManufacturers');
                            if(count($aListmanufacter))
                            {
                                foreach($aListmanufacter as $manufacter)
                                {
                                    if($manufacter['name'] != $template->get('manufacturer'))
                                    { ?>
                                    <option value="<?php echo $manufacter['uri'] ?>"><?php echo $manufacter['name'] ?></option>
                                    <?php
                                    }
                                    else
                                    {?>
                                    <option selected="true" value="<?php echo $manufacter['uri'] ?>"><?php echo $manufacter['name'] ?></option>
                                    <?php } 
                                }
                        } ?>
                    </select>
                    Đơn Giá : <input type="text" value="<?php echo $template->get('price') ?>" name="dongia"></input>
                </p>
                <p>Địa Điểm :<input id="location" value="<?php echo $template->get('location') ?>" hidden="true"></input></p>
                <?php
                    $aParamLocation = array(
                        'id' =>'location_id',
                        'aValue' => $template->get('aLocations'),
                        'stringTitle' => 'name',
                        'stringKey' => 'name',
                        'value' =>  $template->get('location'),
                        'title' =>  $template->get('location')
                    );
                    includeBlock('block/customComboboxBlock',$aParamLocation);
                    $param = array(
                        'category_uri' => $template->get('category_uri'),
                        'category_name' => $template->get('category_name'),
                        'list_categories' => $template->get('list_categories')
                    );
                    includeBlock('block/categoryBlock',$param);
                ?>
            </div>
            <!--<div><p>Mô tả sản phẩm : <input hidden="true" type="text" name="dm_name" id="dm_name_id" value="</?php echo $aListProduct[1]->madm_id ?>"></input></p></div>-->
            <div> <textarea class="" id="summary" name="description" cols="20" rows="20" style="width: 550px;" ><?php echo $template->get('description') ?></textarea></div>
        </div>
    </div>
    <div style="clear: both;"></div><br>
    <div style="float: right;margin-right: 50px;">
        <input type="text" hidden="true" name="title">
        <input class="dangspbutton" type="submit" value="Đồng Ý">
    </div>
    <div style="clear: both;"></div>
</form>
<iframe id="upload_target" name="upload_target" src="" style="display: none;"></iframe>